package com.feng.controller;

import com.feng.entity.RestResponse;
import com.feng.entity.vo.*;
import com.feng.service.AccountService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/account")
public class AccountController {

    final
    AccountService accountService;

    public AccountController(AccountService accountService) {
        this.accountService = accountService;
    }

    @GetMapping("/getAccountAvatar")
    public RestResponse getAccountAvatar() {
        return accountService.getAccountAvatar();
    }

    @GetMapping("/getAccountAndArticle/{id}")
    public RestResponse getAccountAndArticleById(@PathVariable String id) {
        return accountService.getAccountAndArticleById(id);
    }

    @PreAuthorize("@ps.hasPermission('system:user:query')")
    @PostMapping("/list")
    public RestResponse getList(@RequestBody AccountListVo accountList){
        return accountService.getList(accountList);
    }

    @PreAuthorize("@ps.hasPermission('system:user:edit')")
    @PutMapping("/updateStatus")
    public RestResponse updateAccount(@RequestBody UpdateStatusReqVo updateStatus){
        return accountService.updateAccountStatus(updateStatus);
    }

    @PreAuthorize("@ps.hasPermission('system:user:add')")
    @PostMapping("/addAccount")
    public RestResponse addAccount(@RequestBody AccountAddVo accountAdd){
        return accountService.addAccount(accountAdd);
    }

    @PreAuthorize("@ps.hasPermission('system:user:query')")
    @GetMapping("/{id}")
    public RestResponse getAccountById(@PathVariable String id){
        return accountService.getAccountById(id);
    }

    @PreAuthorize("@ps.hasPermission('system:user:edit')")
    @PutMapping("/updateAccount")
    public RestResponse updateAccount(@RequestBody AccountAddVo accountAdd){
        return accountService.updateAccount(accountAdd);
    }

    @PreAuthorize("@ps.hasPermission('system:user:remove')")
    //单个删除
    @DeleteMapping("/{id}")
    public RestResponse delAccount(@PathVariable String id){
        return accountService.delAccount(id);
    }

    @PreAuthorize("@ps.hasPermission('system:user:remove')")
    //批量删除
    @DeleteMapping("/delBatchAccount/{ids}")
    public RestResponse delBatchAccount(@PathVariable List<String> ids){
        return accountService.delBatchAccount(ids);
    }

    //用户数据导出
    @PreAuthorize("@ps.hasPermission('system:user:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        accountService.export(response);
    }

    @GetMapping("/getUserInfo")
    public RestResponse getUserInfo(){
        return accountService.getUserInfo();
    }

    @PreAuthorize("@ps.hasPermission('system:user:edit')")
    @PutMapping("/updateAccountOneself")
    public RestResponse updateAccountOneself(@RequestBody UserInfoUpdateVo userInfo){
        return accountService.updateAccountOneself(userInfo);
    }
}
